#include <bits/stdc++.h>
#define sc(x) scanf("%lld", &(x))
#define pr(x) printf("%lld\n", (x))
#define rep(i, l, r) for (int i = l; i <= r; ++i)
using namespace std;
typedef long long ll;
const int N = 1e4 + 7;
const int mod = 1e9 + 7;
struct person {
    string name;
    int h;
    bool operator<(const person& o) const {
        if (h != o.h) return h > o.h;
        return name < o.name;  // be ordered in alphabetical (increasing)
    }
} a[N];
void optdq(deque<string>& a) {
    bool f = 0;
    while (!a.empty()) {
        if (f)
            cout << ' ';
        else
            f = 1;
        cout << a.front();
        a.pop_front();
    }
    cout << '\n';
}
signed main() {
    int n, k;
    cin >> n >> k;
    rep(i, 1, n) cin >> a[i].name >> a[i].h;

    int r = n % k, m = n / k;
    sort(a + 1, a + 1 + n);

    // first line
    deque<string> dq;
    int p = 0;
    bool L = 0;
    while (dq.size() < m + r) {
        if (L)
            dq.push_front(a[++p].name);
        else
            dq.push_back(a[++p].name);
        L ^= 1;
    }
    optdq(dq);

    // every line
    while (p < n) {
        L = 0;
        while (dq.size() < m) {
            if (L)
                dq.push_front(a[++p].name);
            else
                dq.push_back(a[++p].name);
            L ^= 1;
        }
        optdq(dq);
    }
    return 0;
}